import * as API from '../../../api/orderApi';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    searchkey: '',
    pageNo: 1,
    pageSize: 20,
    total: 0,
    showRecentSearch: true,
    recentSearchRecord: [],
    dataSource: [],
    showOrderFinishedDialog: false,
    showOrderCancelDialog: false,
    curOrderId: '',
    curitemIdx: 0,
    hasRequest: false,
    // hiddenPrice:false
  },
  setSearchKey: function (value) {
    this.setData({
      searchkey: value || ''
    })
  },
  searchRecordClick: function (e) {
    this.setSearchKey(e.target.dataset.text);
    this.setData({
      pageNo: 1,
      pageSize: 20
    })
    // this.addRecentRecord();
    this.loadData(true);
  },
  search(e) {
    let searchkey = e.detail || '';
    if (!searchkey) {
      this.toast('请输入搜索关键字');
      return false;
    }
    this.setData({
      pageNo: 1,
      pageSize: 20,
      searchkey: searchkey.replace(/^\s+|\s+$/g, ""),
      dataSource: []
    })
    this.loadData(true);
    // if (e.detail)
      // this.addRecentRecord();

  },
  hideRecentRecord: function (e) {
    this.showRecentRecord(e.detail);
  },
  clearSearch(e) {
    this.showRecentRecord(e.detail);
  },
  showRecentRecord: function (str) {
    if (str.length == 0) {
      this.setData({
        dataSource: [],
        showRecentSearch: true,
        hasRequest: false
      });
      wx.setNavigationBarTitle({
        title: '订单搜索'
      })
      // this.getRecentRecord();
    } else if (str == 'false')
      this.setData({
        recentSearchRecord: [],
        showRecentSearch: false
      })

  },
  cancelOrder: function (e) {
    let that = this;
    const idx = e.detail.index;
    const id = e.detail.id;
    const key = "dataSource[" + idx + "].deliverStatus";
    this.setData({
      showOrderCancelDialog: true,
      curOrderId: id,
      curitemIdx: idx
    })
  },
  finishOrder: function (e) {
    const idx = e.detail.index;
    const id = e.detail.id;
    this.setData({
      showOrderFinishedDialog: true,
      curOrderId: id,
      curitemIdx: idx
    })
  },
  finishOrderFormSubmit: function (e) { //完成订单弹框
    let that = this;
    if (e.detail.type == 'submit') {
      const key = "dataSource[" + this.data.curitemIdx + "].deliverStatus";
      API.finishOrder({
        id: this.data.curOrderId
      }).then(res => {
        that.setData({
          [key]: 1
        })
      }, err => {

      })
    }
    this.setData({
      showOrderFinishedDialog: false
    })
  },
  cancelOrderFormSubmit: function (e) { //取消订单弹框
    let that = this;
    if (e.detail.type == 'submit') {
      const key = "dataSource[" + this.data.curitemIdx + "].deliverStatus";
      API.cancelOrder({
        id: this.data.curOrderId,
        cancelReason: e.detail.value
      }).then(res => {
        that.setData({
          [key]: 2
        })
      }, err => {

      })
    }
    this.setData({
      showOrderCancelDialog: false
    })

  },
  clearRecentRecord: function () {
    API.clearSearchRecord().then(res => {
      this.showRecentRecord('false');
      // this.getRecentRecord();
    }, err => {

    })
  },
  // addRecentRecord: function () {
  //   API.addSearchRecord({
  //     content: this.data.searchkey
  //   }).then(res => {

  //   }, err => {

  //   })
  // },
  // getRecentRecord: function () {
  //   API.getRecentSearchOrder().then(res => {
  //     this.setData({
  //       recentSearchRecord: res.orderList,
  //       showRecentSearch: true
  //     })
  //   }, err => {

  //   })
  // },
  loadData: function (options) {
    const that = this;
    const postBody = {
      orderNo: this.data.searchkey,
      pageNumber: this.data.pageNo,
      pageSize: this.data.pageSize,
      sort:0
    };
    this.setData({
      hasRequest: false
    })
    this.showRecentRecord('false');
    API.getOrderList(postBody).then(res => {
      let dataList = res.data.records || [];
      if (!options) {
        dataList = that.data.dataSource.concat(res.data.records);
      }
      let hasMore = dataList.length < res.totalCount;
      wx.setNavigationBarTitle({
        title: '搜索结果'
      });
      that.setData({
        isShowReloadBtn: false,
        dataSource: dataList,
        pageNo: that.data.pageNo,
        total: res.totalCount,
        hasRequest: true,
        hasMore: hasMore
      });
    }, err => {
      that.setData({
        isShowReloadBtn: true
      });
    })
  },
  toast: function (tip) {
    wx.showToast({
      title: tip || '网络请求出错,请稍后再试',
      icon: 'none',
      duration: 1500
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getRecentRecord();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var hasMore = this.data.dataSource.length < this.data.total;
    if (this.data.showRecentSearch) //显示历史记录界面，防止获取订单数据
      return false;
    if (hasMore) {
      this.setData({
        pageNo: ++this.data.pageNo
      });
      this.loadData(false);
    }
  },


})